//
//  HMProductList.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/12.
//  Copyright © 2017年 homai. All rights reserved.
//

#define Marin 30

#define KMarin 15

#define kHeight 49

#import "HMProductList.h"

#import "HomeCategory.h"
@interface HMProductList ()

NS_STRONG UIScrollView *listScr;

@end

@implementation HMProductList

- (void)setDataArray:(NSArray *)dataArray {
    [_listScr.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _dataArray = dataArray;
    NSInteger totalWidth = 0;
    NSInteger lastX = 0;
    for (int i = 0; i < _dataArray.count; i++) {
        HomeCategoryDetail *cat = _dataArray[i];
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]};  //指定字号
        CGRect rect = [cat.cat_name boundingRectWithSize:CGSizeMake(0, KWidthSCREEN)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dic context:nil];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(KMarin + Marin * i + totalWidth, 0, rect.size.width, kHeight);
        [btn setTitle:cat.cat_name forState:UIControlStateNormal];
        btn.tag = 200 + i;
        btn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_listScr addSubview:btn];
        
        //        if (i == _selectIndex) {
        //            [btn setTitleColor:kMainColor forState:UIControlStateNormal];
        //        } else {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        }
        
        totalWidth = totalWidth + rect.size.width;
        lastX = CGRectGetMaxX(btn.frame);
    }
    _listScr.contentSize = CGSizeMake(lastX, kHeight);
    [self addSubview:_listScr];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, KWidthSCREEN, kHeight);
        _listScr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _listScr.showsHorizontalScrollIndicator = NO;
        _listScr.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    for (int i = 0; i < _dataArray.count; i++) {
        HomeCategoryDetail *cat = _dataArray[i];
        if ([cat.id integerValue] == selectIndex) {
            UIButton *cliclkbtn = (UIButton *)[self viewWithTag:i + 200];
            [cliclkbtn setTitleColor:kMainColor forState:UIControlStateNormal];
        } else {
            UIButton *cliclkbtn = (UIButton *)[self viewWithTag:i + 200];
            [cliclkbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    _selectIndex = selectIndex;
}

- (void)clickBtn:(UIButton *)sender {
    [sender setTitleColor:kMainColor forState:UIControlStateNormal];
    
    UIButton *btn = (UIButton *)[self viewWithTag:_selectIndex + 200];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    _selectIndex = sender.tag - 200;
    
    if ([_delegate respondsToSelector:@selector(selectType:selectIndex:)]) {
        [_delegate selectType:_dataArray[sender.tag - 200] selectIndex:sender.tag - 200];
    }
}

@end
